<template>
  <div>
    <el-card style="margin: 20px 0px">
      <CategorySelect @getCategoryId="getCategoryId" :show="scene != 0"></CategorySelect>
    </el-card>
    <el-card>
      <!-- 展示SPU列表 -->
      <div v-show="scene == 0">
        <el-button type="primary" icon="el-icon-plus" :disabled="!category3Id" @click="addSpu">添加SPU</el-button>
        <el-table style="width: 100%" border :data="records">
          <el-table-column type="index" label="序号" width="80" align="center">
          </el-table-column>
          <el-table-column prop="spuName" label="spu名称" width="300">
          </el-table-column>
          <el-table-column prop="description" label="spu描述" width="width">
          </el-table-column>
          <el-table-column prop="prop" label="操作" width="width">
            <template slot-scope="{row}">
              <hint-button type="success" icon="el-icon-plus" size="mini" title="添加sku" @click="addSku(row)"></hint-button>
              <hint-button type="warning" icon="el-icon-edit" size="mini" title="修改spu" @click="updateSpu(row)"></hint-button>
              <hint-button type="info" icon="el-icon-info" size="mini" title="查看当前全部spu列表" @click="handler(row)"></hint-button>
              <el-popconfirm title="这是一段内容确定删除吗？" @onConfirm="deleteSpu(row)">
                <hint-button type="danger" icon="el-icon-delete" size="mini" title="删除spu" slot="reference"></hint-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          style="text-align: center"
          :current-page="page"
          :page-sizes="[3, 5, 10]"
          :page-size="limit"
          layout="prev, pager, next, jumper, ->, sizes, total"
          :total="total"
          @current-change="getSpuList"
          @size-change="handleSizeChange"
        >
        </el-pagination>
      </div>
      <!-- 添加SPU|修改SPU -->
      <SpuForm v-show="scene == 1" ref="spu" @changeScene="changeScene"></SpuForm>
      <!-- 添加SKU -->
      <SkuForm v-show="scene == 2" ref="sku" @changeScene1="changeScene1"></SkuForm>
    </el-card>
    <!-- before-close	关闭前的回调 会暂停 Dialog 的关闭 -->
    <el-dialog :title="`${spu.spuName} 的sku列表`" :visible.sync="dialogTableVisible" :before-close="close">
      <el-table :data="skuList" style="width: 100%" v-loading="loading">
        <el-table-column prop="skuName" label="名称" width="width" border>
        </el-table-column>
        <el-table-column prop="price" label="价格" width="width" border>
        </el-table-column>
        <el-table-column prop="weight" label="重量" width="width" border>
        </el-table-column>
        <el-table-column label="默认图片" width="width" border>
          <template slot-scope="{row}">
            <el-image style="width: 100px; height: 100px" :src="row.skuDefaultImg"></el-image>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import SpuForm from '@/views/products/Spu/SpuForm'
import SkuForm from '@/views/products/Spu/SkuForm'

export default {
  name: "Spu",
  components: {
    SpuForm,
    SkuForm
  },
  data() {
    return {
      category1Id: "",
      category2Id: "",
      category3Id: "",
      // 控制三级联动的可操作性
      show: true,
      // 分页器当前页
      page: 1,
      // 一页展示数据数
      limit: 3,
      // spu列表数据
      records: [],
      // 展示的数据总条数
      total: 0,
      // 0SPU列表展示 1添加SPU|修改SPU 2添加SKU
      scene: 0,
      // 控制对话框的显示与隐藏
      dialogTableVisible: false,
      spu: {},
      // 存储sku列表数据
      skuList: [],
      loading: true
    };
  },
  methods: {
    // 三级联动的自定义事件
    getCategoryId({ categoryId, level }) {
      if (level == 1) {
        this.category1Id = categoryId;
        this.category2Id = "";
        this.category3Id = "";
      } else if (level == 2) {
        this.category2Id = categoryId;
        this.category3Id = "";
      } else {
        this.category3Id = categoryId;
        // 发请求获取平台属性的数据
        this.getSpuList();
      }
    },
    // 获取SPU列表数据的方法
    async getSpuList(pages = 1) {
      // console.log(pages);
      this.page = pages;
      const {page, limit, category3Id} = this;
      let result = await this.$API.spu.reqSpuList(page, limit, category3Id);
      // console.log(result);
      if(result.code == 200) {
        this.total = result.data.total;
        this.records = result.data.records;
      }
    },
    // 每页展示数据的条数变化的回调
    handleSizeChange(limit) {
      // console.log(limit);
      this.limit = limit;
      this.getSpuList();
    },
    // 添加SPU按钮的回调
    addSpu() {
      this.scene = 1;
      // 通知子组件SpuForm发请求
      this.$refs.spu.addSpuData(this.category3Id);
    },
    // 修改SPU按钮的回调
    updateSpu(row){
      this.scene = 1;
      // 获取子组件SpuForm的 在父组件中可以通过$refs获取子组件
      // console.log(this.$refs.spu);
      this.$refs.spu.initSpuData(row);
    },
    // SpuForm自定义事件的回调
    changeScene({scene, flag}) {
      // 切换结构
      this.scene = scene;
      // 子组件通知父组件切换场景 要再次获取SPU数据展示 并停留在当前页
      if(flag == '修改') {
        this.getSpuList(this.page);
      }else {
        this.getSpuList();
      }
    },
    // 删除SPU按钮的回调
    async deleteSpu(row) {
      let result = await this.$API.spu.reqDeleteSpu(row.id);
      // console.log(result);
      if(result.code == 200) {
        this.$message({type: 'success', message: '删除成功'});
        this.getSpuList(this.records.length > 1 ? this.page : this.page - 1);
      }
    },
    // 添加SKU按钮的回调
    addSku(row) {
      // console.log(row);
      // 切换场景
      this.scene = 2;
      // 父组件调用子组件SkuForm的方法 让子组件发三个请求
      this.$refs.sku.getData(this.category1Id, this.category2Id, row);
    },
    // SkuForm自定义事件的回调
    changeScene1(scene) {
      this.scene = scene;
    },
    // 查看全部SKU列表按钮的回调
    async handler(spu) {
      // 显示对话框
      this.dialogTableVisible = true;
      // 存储spu信息
      this.spu = spu;
      // 获取sku列表数据
      let result = await this.$API.spu.reqSkuList(spu.id);
      console.log(result);
      if(result.code == 200) {
        this.skuList = result.data;
        // 隐藏loading
        this.loading = false;
      }
    },
    // 对话框关闭前的回调
    close(done) {
      this.loading = true;
      this.skuList = [];
      // 关闭对话框
      done();
    }
  },
  
};
</script>

<style></style>
